from sqlalchemy import (
    BigInteger,
    Boolean,
    Column,
    DateTime,
    Enum,
    Float,
    Integer,
    String,
    func,
)
from sqlalchemy.orm import relationship

from app.database.models.base import Base
from app.database.models.m2ms import proxies_users


class User(Base):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True)
    telegram_id = Column(BigInteger, unique=True)
    username = Column(String(50))
    is_banned = Column(Boolean, server_default="0")
    is_registered = Column(Boolean, server_default="0")
    # has_subscription = Column(Boolean, server_default="0")
    balance = Column(Float, server_default="0")
    email = Column(String(100))
    orders = relationship("Order", back_populates="user")
    proxies = relationship("Proxy", secondary=proxies_users, back_populates="users")
    allowed_ips = relationship("AllowedIP", back_populates="user")
    payments = relationship("Payment", back_populates="user")
    lang_code = Column(Enum("en", "ru"), server_default="ru")
    test_proxy_acquired = Column(DateTime)
    # subscribed_until = Column(DateTime())
    was_registered = Column(DateTime, server_default=func.now())

    def __str__(self):
        return str(self.telegram_id)

    def __repr__(self):
        return f"<TgUser №{self.id} {self.telegram_id}>"
